﻿CREATE PROCEDURE [acms].[ControlType_Put]
	@Id int,
	@SiteId int,
	@Name varchar(50),
	@AdminControlType varchar(255),
	@AdminEditControlType varchar(255),
	@AdminDisplayControlType varchar(255),
	@FrontEndControlType varchar(255),
	@Custom int,
	@Notes varchar(1000),
	@VersionId timestamp,
	@IdOut int output,
	@VersionIdOut timestamp output
	
AS
	if exists (select null from [ControlType_View] with (nolock) where Id = @Id) begin
		update [ControlTypes]
		set
			SiteId = @SiteId,
			Name = @Name,
			AdminControlType = @AdminControlType,
			AdminEditControlType = @AdminEditControlType,
			AdminDisplayControlType = @AdminDisplayControlType,
			FrontEndControlType = @FrontEndControlType,
			Custom = @Custom,
			Notes = @Notes
		where
			Id = @Id
			and VersionId = @VersionId
			
		if @@Rowcount = 0
			return -100
		else
			set @IdOut = @Id
	end
	else if (@Id is null or @Id <= 0) begin
		insert into [ControlTypes]
			(SiteId, Name, AdminControlType, AdminEditControlType, AdminDisplayControlType, FrontEndControlType, Custom, Notes)
		values
			(@SiteId, @Name, @AdminControlType, @AdminEditControlType, @AdminDisplayControlType, @FrontEndControlType, @Custom, @Notes)
		set @IdOut = @@Identity
	end
	else
		return -200
	
	select @VersionIdOut = VersionId From [ControlTypes] with (nolock) where Id = @Id
RETURN 0;